package com.example.administrator.daggerdemo;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.daggerdemo.base2.base.BaseActivity;
import com.example.administrator.daggerdemo.model.GrowDianzanBean;
import com.example.administrator.daggerdemo.model.network.response.LoginResponseInfo;
import com.example.administrator.daggerdemo.presenter.GrowthActivityPresenter;
import com.example.administrator.daggerdemo.presenter.MainActivityPresenter;

import com.example.administrator.daggerdemo.view.IGrowthActivityView;
import com.example.administrator.daggerdemo.view.IMainActivityView;

import java.util.List;

/**
 * Created by zhengjian on 2016/12/13.
 */
public class GrothListAcvitivity extends BaseActivity implements IGrowthActivityView,IMainActivityView,BaseQuickAdapter.RequestLoadMoreListener {

    MainActivityPresenter loginPresenter;
    GrowthActivityPresenter growthActivityPresenter;
    RecyclerView growthView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.growthactivity);
        growthView=(RecyclerView)findViewById(R.id.growthrecy);
        initView();
        initData();

    }

    @Override
    protected void initData() {
        super.initData();
        doLogin();
    }

    int pageIndex=1;
    int pageSize=5;
    int totalSize=0;
    int mCurrentCounter=0;
    @Override
    public void getGrowthList(int pageindex) {
          growthActivityPresenter.getGrothList(pageindex,pageSize);
    }

    @Override
    public void GrowthListSuccess(final List<GrowDianzanBean> list,int totalList) {
        if(pageIndex==1)
        {
            totalSize=totalList;
            adapter.setNewData(list);
        }else
        {
            growthView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    adapter.addData(list);
                    mCurrentCounter = adapter.getData().size();
                    adapter.loadMoreComplete();
                }
            },1000);
        }
    }

    @Override
    public void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        adapter.loadMoreFail();
    }

    @Override
    public void showProgress() {
        Toast.makeText(this, "开始请求", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void hideProgress() {
        Toast.makeText(this, "结束请求", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void msgError(String errorString) {
        Toast.makeText(this, errorString, Toast.LENGTH_SHORT).show();
        adapter.loadMoreFail();
    }

    @Override
    public void doLogin() {
        loginPresenter.doLogin("13243762775","123456");
    }

    @Override
    public void loginSuccess(LoginResponseInfo info) {
     MyApplication.DOTOKEN=info.getUser_token();
        MyApplication.PHONE=info.getUser_phone();
        getGrowthList(1);
    }

    @Override
    protected void initPresenter() {
        loginPresenter=new MainActivityPresenter(this);
        growthActivityPresenter=new GrowthActivityPresenter(this);
    }
    GrowthAdapter adapter;
    @Override
    protected void initView() {
        adapter=new GrowthAdapter(null);
        growthView.setLayoutManager(new LinearLayoutManager(this));
        adapter.setOnLoadMoreListener(this);
        growthView.setAdapter(adapter);
    }

    @Override
    public void onLoadMoreRequested() {
        if(mCurrentCounter>=totalSize)
        {
            adapter.loadMoreEnd(false);
        }else
        {
            getGrowthList(pageIndex++);
        }
    }


    class GrowthAdapter extends BaseQuickAdapter<GrowDianzanBean,BaseViewHolder>
    {
        public GrowthAdapter(List<GrowDianzanBean> data) {
            super(R.layout.growthitem,data);
        }

        @Override
        protected void convert(BaseViewHolder holder, GrowDianzanBean growDianzanBean) {
            holder.setText(R.id.name,growDianzanBean.getFamilyName());
        }
    }
}
